package com.hdw.auth.controller;

import com.hdw.auth.api.SysFucaService;
import com.hdw.auth.bean.model.SysFuca;
import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.util.TreeUtil;
import com.hdw.common.vo.LayTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sysFuca控制器
 * Created by hozeData on 2021/11/21.
 */
@RestController
@RequestMapping("/auth/sysFuca/")
@Api(tags="功能分类")
public class SysFucaController {

    @Autowired
    SysFucaService sysFucaService;

    @ApiOperation(value="查询")
    @RequestMapping(value="findByModel", method=RequestMethod.POST)
    public BaseResult findByModel(@RequestBody(required = false) SysFuca sysFuca) {
        List<SysFuca> fucaList = sysFucaService.findByModel(sysFuca);
        return new BaseResult(CommonResultConstant.SUCCESS, fucaList);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="findTree", method=RequestMethod.POST)
    public BaseResult findTree(@RequestBody(required = false) SysFuca sysFuca) {
        List<SysFuca> fucaList = sysFucaService.findByModel(sysFuca);
        Map<String,LayTreeVO> treeMap = new HashMap<>();
        LayTreeVO tree;
        for(SysFuca fuca:fucaList) {
            tree = new LayTreeVO();
            tree.setId(fuca.getId()+"");
            tree.setTitle(fuca.getNa());
            tree.setParent(fuca.getIdFa()==null?"":fuca.getIdFa()+"");
            tree.setData(fuca);
            treeMap.put(fuca.getId()+"",tree);
        }
        List<LayTreeVO> treeList = TreeUtil.treeHandler(treeMap);
        return new BaseResult(CommonResultConstant.SUCCESS, treeList);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="sysFuca", value="实体", paramType="body", required=true, dataType="SysFuca")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody SysFuca sysFuca, HttpServletRequest request)  {
        if(sysFuca.getId()==null) {
            sysFucaService.insertSelective(sysFuca);
        } else {
            sysFucaService.updateByIdSelective(sysFuca);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, null);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="id", value="主键", paramType="query", required=true, dataType="Integer")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Integer id, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("请选择要编辑的项");
        }
        SysFuca sysFuca = sysFucaService.getById(id);
        return new BaseResult(CommonResultConstant.SUCCESS, sysFuca);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="id", value="主键", paramType="query", required=true, dataType="Integer")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Integer id, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("请选择要删除的项");
        }
        int num = sysFucaService.deleteById(id);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
